<html>
<head><meta charset="utf-8"><title>current test · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html">current test</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240578366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/240578366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Lattuada <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#240578366">(May 28 2021 at 09:04)</a>:</h4>
<p>Hi folks, do you know if there's some built-in way to determine what test crate and test file (in <code>tests/</code> is currently being run? I need to (optionally) emit some diagnostics to a file (too verbose for stdout/stderr) and I'd like to be able to associate it with the test. I imagine I can set up a macro that achieve (most of) this, but it'd be cool if it was available already.</p>



<a name="240585572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/240585572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#240585572">(May 28 2021 at 10:16)</a>:</h4>
<p>You can get the file with <code>file!</code>. I don't know how to get the crate name.</p>



<a name="240591989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/240591989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#240591989">(May 28 2021 at 11:36)</a>:</h4>
<p>cargo sets some envvars.</p>



<a name="240592011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/240592011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#240592011">(May 28 2021 at 11:36)</a>:</h4>
<p>But relying on them in tests is quite nasty - complicates uses without cargo.</p>



<a name="240594625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/240594625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Lattuada <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#240594625">(May 28 2021 at 12:06)</a>:</h4>
<blockquote>
<p>cargo sets some envvars</p>
</blockquote>
<p>Ah, interesting, may look into that. This is essentially a convenience for test-driven-development, so I'm not too worried about uses without cargo. Thanks!</p>



<a name="240653059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/240653059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#240653059">(May 28 2021 at 19:54)</a>:</h4>
<p>I think <code>module_path!</code> includes the crate name</p>



<a name="243311246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243311246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Huszagh (He/Him) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243311246">(Jun 20 2021 at 16:58)</a>:</h4>
<p>General undefined behavior question: is a a pointer comparison outside of the same array undefined behavior in Rust like it is in C/C++ (and I believe LLVM as well).</p>
<p>The offending code I'm working with is another library I'm checking for any issue with correctness/safety, and I found the following offending code:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">ByteSlice</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">ptr</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">end</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">_marker</span>: <span class="nc">PhantomData</span><span class="o">&lt;&amp;'</span><span class="na">a</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="n">ByteSlice</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">check_len</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">n</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">ptr</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">end</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>In C/C++, <code>self.ptr.add(n) &lt;= self.end</code> would be undefined behavior is <code>n</code> is greater than <code>self.end - self.ptr</code>.</p>



<a name="243311832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243311832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243311832">(Jun 20 2021 at 17:11)</a>:</h4>
<p>Yes, it is UB: <a href="https://doc.rust-lang.org/std/primitive.pointer.html#safety-5">https://doc.rust-lang.org/std/primitive.pointer.html#safety-5</a></p>



<a name="243311928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243311928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243311928">(Jun 20 2021 at 17:13)</a>:</h4>
<p>Not because of the comparison, but just because you constructed the out of bounds pointer in the first place</p>



<a name="243311939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243311939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243311939">(Jun 20 2021 at 17:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="153718">Alexander Huszagh (He/Him)</span> <a href="#narrow/stream/122651-general/topic/current.20test/near/243311246">said</a>:</p>
<blockquote>
<p><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="n">ByteSlice</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">check_len</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">n</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">ptr</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">end</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div><br>
</p>
</blockquote>
<p>If <code>n</code> is untrusted, it's UB before even getting to the <code>&lt;=</code> because the <code>add</code> needs to be inbounds to be sound.</p>
<p>(Pointer comparison in Rust is safe, so cannot ever be UB, but it's complex to use the result in a meaningful way.)</p>



<a name="243312165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243312165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Huszagh (He/Him) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243312165">(Jun 20 2021 at 17:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/current.20test/near/243311832">said</a>:</p>
<blockquote>
<p>Yes, it is UB: <a href="https://doc.rust-lang.org/std/primitive.pointer.html#safety-5">https://doc.rust-lang.org/std/primitive.pointer.html#safety-5</a></p>
</blockquote>
<p>Perfect, that was my assumption as well. I'm glad I audited this.</p>
<p>So the correct way would be this, correct?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">check_len</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">n</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">end</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">ptr</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">n</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="n">len</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Or is there a better approach? <code>self.end</code> and <code>self.ptr</code> can be guaranteed to be of the same array (well, end is 1-past-the-end of the array, which is still well-defined behavior).</p>



<a name="243312273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243312273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Huszagh (He/Him) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243312273">(Jun 20 2021 at 17:21)</a>:</h4>
<p>Ah nevermind, offset_from exists.</p>



<a name="243312927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243312927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243312927">(Jun 20 2021 at 17:37)</a>:</h4>
<p>Here's what <code>slice::Iter</code> does: <a href="https://github.com/rust-lang/rust/blob/2e940ac5e97ba4424be5a8e02ff4c7846ca1e25a/library/core/src/slice/iter/macros.rs#L14-L38">https://github.com/rust-lang/rust/blob/2e940ac5e97ba4424be5a8e02ff4c7846ca1e25a/library/core/src/slice/iter/macros.rs#L14-L38</a></p>
<p>If you're only dealing in bytes, though, that complication is unnecessary.</p>



<a name="243313169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243313169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243313169">(Jun 20 2021 at 17:42)</a>:</h4>
<p>Wouldn't <code>wrapping_add</code> work here? It specifically does not require the pointer be valid until it is dereferenced</p>



<a name="243313250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243313250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243313250">(Jun 20 2021 at 17:44)</a>:</h4>
<p><a href="https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_add">https://doc.rust-lang.org/std/primitive.pointer.html#method.wrapping_add</a><br>
The paragraph on how it compares to <code>add</code> implies it is fine to compare the result as long as you're not dereferencing it</p>



<a name="243314815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243314815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Huszagh (He/Him) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243314815">(Jun 20 2021 at 18:20)</a>:</h4>
<p><span class="user-mention" data-user-id="319144">@CraftSpider</span> That looks interesting, yeah.</p>



<a name="243317029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243317029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243317029">(Jun 20 2021 at 19:15)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kt">isize</span>::<span class="n">try_from</span><span class="p">(</span><span class="n">n</span><span class="p">).</span><span class="n">ok</span><span class="p">().</span><span class="n">map_or</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">end</span><span class="p">.</span><span class="n">offset_from</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">ptr</span><span class="p">)</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
</code></pre></div>
<p>This has the advantage of working even for slices of <code>T</code> where <code>size_of::&lt;T&gt;() &gt; 1</code></p>



<a name="243324812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243324812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243324812">(Jun 20 2021 at 22:26)</a>:</h4>
<p>But I think it doesn't work for ZSTs, since those can be longer than <code>usize::MAX</code>.</p>
<p><code>offset_from</code> seems like the right choice, so long as it's known that both pointers are part of the same allocated object.</p>



<a name="243324897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243324897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Huszagh (He/Him) <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243324897">(Jun 20 2021 at 22:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/122651-general/topic/current.20test/near/243324812">said</a>:</p>
<blockquote>
<p>But I think it doesn't work for ZSTs, since those can be longer than <code>usize::MAX</code>.</p>
<p><code>offset_from</code> seems like the right choice, so long as it's known that both pointers are part of the same allocated object.</p>
</blockquote>
<p>Yep, thinking this is the best solution. This is part of my (delayed) efforts to integrate fast-float-rust into core. Basically, the library has a custom slice type for better iteration efficiency, just ensuring there's no UB in it anywhere prior to submitting a PR.</p>



<a name="243339964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243339964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243339964">(Jun 21 2021 at 04:57)</a>:</h4>
<p>What's wrong with <code>[u8]</code> or <code>slice::Iter&lt;'a, u8&gt;</code>?  How's the custom one faster?</p>



<a name="243371029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/current%20test/near/243371029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/current.20test.html#243371029">(Jun 21 2021 at 11:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/122651-general/topic/current.20test/near/243324812">said</a>:</p>
<blockquote>
<p>But I think it doesn't work for ZSTs, since those can be longer than <code>usize::MAX</code>.</p>
</blockquote>
<p>Yep, I did mean that <code>&gt; 1</code> part: on top of that, <code>.offset_from</code> panics for Zsts! So a <code>if ::mem::size_of::&lt;T&gt;() == 0 { return true; }</code> branch ought to be added should support for Zsts be warranted.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>